/**
 * 并发请求控制
 */
class RendRequest {
  #queue;
  #count; // 并发请求池
  #maxCount = 3; // 最大并发数

  constructor() {
    this.#queue = [];
    this.#count = 0;
  }
  execute(config) {
    if (this.#count++ < this.#maxCount) {
      return Promise.resolve(config);
    }
    return new Promise((resolve) => this.#queue.push(() => resolve(config)));
  }
  complete() {
    this.#count--;
    const task = this.#queue.shift();
    task && task();
  }
}

const sendRequest = new RendRequest();
export default sendRequest;
